<?php
/**
 *Create by PhpStorm
 *User:Kendy
 *Date:2019/10/8/008
 *Time:14:19
 */
namespace app\common\model;
use think\Model;
use think\Db;
use app\common\model\UserModel as um;
use app\common\model\LogOrderModel as lo;
class guideOrderModel extends Model
{
    // 确定链接表名
    protected $name = 'guide_order';

    /**
     * 根据搜索条件获取所有的导游订单数据
     * @param $where  查询条件
     * @param $offset
     * @param $limit
     * @return mixed
     */
    public function getGuideOrderList($where ,$offset, $limit)
    {
        $resultData = $this->where($where)->order('order_id desc')->limit($offset, $limit)->select();
        if(count($resultData)>0)
        {
              foreach ($resultData as $key => $v)
              {
                  $resultData[$key]['order_status'] = lvOrderStatus($v['order_status']);
                  $resultData[$key]['username'] = model('UserModel')->getUserInfoById(['uid'=>$v['uid']])['nickname'];
                  $resultData[$key]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
              }
        }
        return $resultData;
    }


    /**
     * 根据搜索条件获取所有的导游订单数量
     * @param $where  查询条件
     * @return mixed
     */
    public function  getGuideOrderCount($where)
    {
        return $this->where($where)->count();
    }

    /**根据搜索条件获取单条的导游订单信息
     * @param $where 查询条件
     * @return mixed
     */
    public function  getGuideOrderById($where)
    {
        $resultData = $this->where($where)->find();
        if(empty($resultData)) return;
        $resultData['pay_type'] = lvPayType($resultData['pay_type']);
        $resultData['order_status'] = lvOrderStatus($resultData['order_status']);
        $userInfo = new um;
        $resultData['username'] = $userInfo->getUserInfoById(['uid'=>$resultData['uid']])['nickname'];
        return $resultData;
    }

    /**
     *根据搜索条件删除单条的导游订单
     * @param $where
     */
    public function  guideDel($where)
    {
        try{
            $this->where($where)->delete();
            return true;

        }catch(\Exception $e){
            return $e->getMessage();
        }
    }


     public function submitGuideOrder(){ 

       $guideId = input('post.guide_id');
       if(empty($guideId))
          return apiReturn('400','导游id为空',0); 
      $rename = input('post.rename');
       if(empty($rename))
          return apiReturn('400','预约人的姓名为空',0); 
      $phone = input('post.phone');
       if(empty($phone))
        return apiReturn('400','预约人的手机号为空',0); 
      $isCar = input('post.is_car',0,'trim');
      $oldMan = input('post.old_man/d',0,'trim');
      $childMan = input('post.child_man/d',0,'trim');
      $startTime = input('post.start_time'); //格式为 XXXX-XX-XX
       if(empty($startTime))
         return apiReturn('400','开始时间为空',0); 
      $endTime = input('post.end_time'); //格式为 XXXX-XX-XX
       if(empty($endTime))
         return apiReturn('400','结束时间为空',0); 
      $userId = input('post.user_id');  
       if(empty($userId))
         return apiReturn('400','用户信息为空',0); 
        $startdate=strtotime($startTime);
        $enddate=strtotime($endTime);
        $days=round(($enddate-$startdate)/3600/24);   
      //查看是否有该导游信息
      $resultData = Db::name('guide')   
      ->field('guide_id,is_cars,guide_money,name')
      ->where(['is_del'=>0,'is_pass'=>1,'guide_id'=>$guideId])
      ->find();   
       if(empty($resultData))
         return apiReturn('400','无该导游信息',0);
       $carPrice = 0;
      if($isCar==1)
      {
         if($resultData['is_cars']==0)
           return apiReturn('400','该导游无带车服务',0);
         $carInfo = Db::name('guide_car')
         ->where(['is_display'=>0,'status'=>0])
         ->find();
         $carPrice = $carInfo['car_money'] * $days;
      } 
      $totalMan = $oldMan + $childMan;
      if($totalMan<=0)
      return apiReturn('400','人数无效',0); 
      $guidePrice =  ($totalMan)*$resultData['guide_money'];
      $order_price = $guidePrice + $carPrice;
      $returnData['name'] = $resultData['name'];
      $returnData['start_time'] = $startTime;
      $returnData['end_time'] = $endTime;
      $returnData['is_car'] = $isCar;
      $returnData['price'] = $order_price;
      
     //生成订单
      Db::startTrans();
      try{
        $orderSn = makeOrderSn(); 
        //创建订单
        $order['order_sn'] = $orderSn;
        $order['guide_id'] = $resultData['guide_id'];
        $order['uid'] = $userId;
        $order['uid'] = $userId;
        $order['car_id'] = $rename;
        $order['old_man'] = $oldMan;
        $order['child_man'] = $childMan;
        $order['rename'] = $rename;
        $order['phone'] = $phone;
        $order['is_car'] = $isCar;
        $order['order_price'] = $order_price; 
        $order['create_time'] = time();
        $order['start_time'] = $startdate;
        $order['end_time'] = $enddate;
        $result = $this->isUpdate(false)->save($order);  
        if(false !== $result)
        {
           $orderId = $this->order_id; 
            //创建订单记录 
            $logOrder = new lo();
            $logOrder->addLogOrder($orderId,4 ,$userId, 0, '下单成功，等待用户付');
            //成交单数+1
            Db::name('guide')->where('guide_id',$resultData['guide_id'])->setInc('sales_volume',1);
             //给用户发送消息提示
            send_user_message(4, 2, $userId, $orderSn,$resultData['name'],$orderId);
            //给商户发送消息
            
        } 
         Db::commit();
         return apiReturn('200','提交订单成功',$returnData);  
      }catch(\Exection $e)
      { 
          Db::rollback();
          return apiReturn('400','提交订单失败',0); 
      }
   }



}